#include "cust_apply_deposit_dialog.h"
#include "ui_cust_apply_deposit_dialog.h"
#include "QSqlDatabase"
#include "QSqlQuery"
#include "QDebug"
#include "QDateTime"
#include "QMessageBox"
#include "bts_mgmt.h"
#include "QDateTime"
#include "QSqlError"

cust_apply_deposit_dialog::cust_apply_deposit_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::cust_apply_deposit_dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("申请存款");
    ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
}

cust_apply_deposit_dialog::~cust_apply_deposit_dialog()
{
    delete ui;
}

void cust_apply_deposit_dialog::on_confirm_clicked()
{
    QSqlDatabase sqlite_dep = QSqlDatabase::database("deposit");
    QSqlQuery query_dep(sqlite_dep);
    //先判断是否开户，如果未开户则不进行存款
    query_dep.prepare("SELECT reviewstatus FROM DEPOSIT WHERE custtelNumber = ?");
    query_dep.addBindValue(ui->user_card_id->text());
    query_dep.exec();
    if(!query_dep.next())
    {
        qDebug() << query_dep.value(0);
        QMessageBox box;
        box.setWindowTitle("warning");
        box.setText("您尚未开户");
        box.setIcon(QMessageBox::Warning);
        box.exec();
        return;
    }

    if(query_dep.value(0).toString() == "待审核")
    {
        QMessageBox box;
        box.setWindowTitle("warning");
        box.setText("您的开户申请还在审核中");
        box.setIcon(QMessageBox::Warning);
        box.exec();
        return;
    }
    //判断如果原来金额不为"未进行储蓄"则不进行储蓄
    query_dep.prepare("SELECT savingsamount FROM DEPOSIT WHERE custtelNumber = ?");
    query_dep.addBindValue(ui->user_card_id->text());
    query_dep.exec();
    query_dep.next();
    if(query_dep.value(0) != "未进行储蓄")
    {
        QMessageBox box;
        box.setWindowTitle("warning");
        box.setText("再次申请储蓄前请先取出存款");
        box.setIcon(QMessageBox::Warning);
        box.exec();
        return;
    }
    query_dep.prepare("UPDATE DEPOSIT SET savingstype = ?, savingsamount = ?, savingstime = ? WHERE custtelnumber = ? ");
    query_dep.addBindValue(ui->comboBox->currentText());
    query_dep.addBindValue(ui->savings_amount->text());
    //此处可自行选择存款时间，为演示利息计算问题
    QDateTime dt = ui->dateTimeEdit->dateTime();
    query_dep.addBindValue(dt.toString());

    query_dep.addBindValue(ui->user_card_id->text());
    query_dep.exec();
    BTS_Mgmt::insertToTransactionDB(ui->user_card_id->text(),"存款",ui->savings_amount->text(),QDateTime::currentDateTime().toString());
    QMessageBox box;
    box.setWindowTitle("completed");
    box.setText("存款成功");
    box.setIcon(QMessageBox::Warning);
    box.exec();

    return;
}
